#include <iostream>
#include <vector>

using namespace std;

class Solution 
{
public:
	int removeDuplicates(vector<int>& nums) 
	{
		if (nums.empty()) 
		{
			return 0;
		}
		
		int slow = 1;  // 直接从索引 1 开始，始终指向唯一元素的下一个位置
		for (size_t fast = 1; fast < nums.size(); fast++) 
		{
			if (nums[fast] != nums[fast - 1])  // nums[fast]是第一次出现的新数
			{
				nums[slow] = nums[fast];	   //fast负责找新数，slow负责收集
				slow++;
			}
		}
		
		return slow;  // 新长度
	}
};

int main() 
{
	Solution sol;
	vector<int> nums = {0, 1, 2, 3, 4, 5, 5, 6};
	
	int k = sol.removeDuplicates(nums);
	
	cout << "去重后的新长度 k = " << k << endl;
	cout << "数组前 k 个唯一元素为: ";
	for (int i = 0; i < k; i++) 
	{
		cout << nums[i] << " ";
	}
	cout << endl;
	
	return 0;
}

